home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Ventriloquist / source code / TalkerApp / PP Server.cp < prev    next >
Encoding:
Text File  |  1997-06-27  |  7.0 KB  |  262 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    PP Basic Starter.cp         ©1994-1997 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    This file contains the starter code for a basic PowerPlant application
  6.  
  7. #include "PP Basic Starter.h"
  8.  
  9. #include <LGrowZone.h>
  10. #include <LWindow.h>
  11. #include <PP_Messages.h>
  12. #include <PP_Resources.h>
  13. #include <PPobClasses.h>
  14. #include <UDrawingState.h>
  15. #include <UMemoryMgr.h>
  16. #include <URegistrar.h>
  17. #include <LEditField.h>
  18. #include "beeper.h"
  19. #include    "speech.proto.h"
  20. #include "OTdsp.h"
  21.  
  22. Boolean            gCanSpeak;
  23. short            gSelectedVoiceIndex;
  24. short            gNumberVoices;
  25. short            gSpeakingVoiceIndex;
  26. Boolean            gSpeakSelectionEnabled;
  27. SpeechChannel    gSpeechChannel;
  28. OSErr            gSpeechErrors[];
  29. Handle            gSpeechText;
  30. VoiceSpec **        gVoices;
  31. long            gConnID;
  32.  
  33. // put declarations for resource ids (ResIDTs) here
  34.  
  35. const ResIDT    window_Sample        = 1;    // EXAMPLE
  36.  
  37. BeeperGlobals    *gInitGlobals;
  38.  
  39. // ===========================================================================
  40. //        • Main Program
  41. // ===========================================================================
  42.  
  43. void main(void)
  44. {
  45.                                     // Set Debugging options
  46.     SetDebugThrow_(debugAction_Alert);
  47.     SetDebugSignal_(debugAction_Alert);
  48.  
  49.     InitializeHeap(3);                // Initialize Memory Manager
  50.                                     // Parameter is number of Master Pointer
  51.                                     //   blocks to allocate
  52.     
  53.                                     // Initialize standard Toolbox managers
  54.     UQDGlobals::InitializeToolbox(&qd);
  55.     
  56.     new LGrowZone(20000);            // Install a GrowZone function to catch
  57.                                     //    low memory situations.
  58.  
  59.     Str31    Server = "\pTalker";
  60.     gConnID = OTdspListen(Server);
  61.     CPPStarterApp    theApp;            // replace this with your App type
  62.     theApp.Run();
  63.     StopSpeaking();    //kill current speech if any
  64.     OTClose(gConnID);
  65. }
  66.  
  67.  
  68. // ---------------------------------------------------------------------------
  69. //        • CPPStarterApp             // replace this with your App type
  70. // ---------------------------------------------------------------------------
  71. //    Constructor
  72.  
  73. CPPStarterApp::CPPStarterApp()
  74. {
  75.     // Register functions to create core PowerPlant classes
  76.     
  77.     RegisterAllPPClasses();
  78. }
  79.  
  80.  
  81. // ---------------------------------------------------------------------------
  82. //        • ~CPPStarterApp            // replace this with your App type
  83. // ---------------------------------------------------------------------------
  84. //    Destructor
  85. //
  86.  
  87. CPPStarterApp::~CPPStarterApp()
  88. {
  89. }
  90.  
  91. // ---------------------------------------------------------------------------
  92. //        • StartUp
  93. // ---------------------------------------------------------------------------
  94. //    This function lets you do something when the application starts up
  95. //    without a document. For example, you could issue your own new command.
  96.  
  97. void
  98. CPPStarterApp::StartUp()
  99. {
  100. //    ObeyCommand(cmd_New, nil);        // EXAMPLE, create a new window
  101.     long    err;
  102. //    SelectorFunctionUPP    ourGestaltUPP;
  103. //    long    versionCheck;
  104.     VoiceDescription    defaultVoiceInfo;
  105. //    err = Gestalt(kDTS_Signature, (long *)&ourGestaltUPP);
  106. //    if (err == noErr) {
  107. //        err = CallSelectorFunctionProc(ourGestaltUPP, gestaltVersion, (long *)&versionCheck);
  108. //        if (versionCheck != 0x0100) err = -1;
  109. //        if (err == noErr) {
  110. //            err = CallSelectorFunctionProc(ourGestaltUPP, kGestaltGetInitGlobals, (long *)&gInitGlobals);
  111.     gCanSpeak = !gCanSpeak;
  112.     //get default voice
  113.     err = GetVoiceDescription(nil, &defaultVoiceInfo, sizeof(defaultVoiceInfo));
  114.     err = NewSpeechChannel(&defaultVoiceInfo.voice, &gSpeechChannel);
  115. //        }
  116. //    }
  117. }
  118.  
  119. // ---------------------------------------------------------------------------
  120. //        • ObeyCommand
  121. // ---------------------------------------------------------------------------
  122. //    Respond to commands
  123.  
  124. Boolean
  125. CPPStarterApp::ObeyCommand(
  126.     CommandT    inCommand,
  127.     void        *ioParam)
  128. {
  129.     Boolean        cmdHandled = true;
  130.  
  131.     switch (inCommand) {
  132.     
  133.         // Deal with command messages (defined in PP_Messages.h).
  134.         // Any that you don't handle will be passed to LApplication
  135.              
  136.         case cmd_New:
  137.                                         // EXAMPLE, create a new window
  138.             LWindow        *theWindow;
  139.             theWindow = LWindow::CreateWindow(window_Sample, this);    
  140.             theWindow->Show();
  141.             break;
  142.  
  143.  
  144.  
  145.         default:
  146.             cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
  147.             break;
  148.     }
  149.     
  150.     return cmdHandled;
  151. }
  152.  
  153. // ---------------------------------------------------------------------------
  154. //        • FindCommandStatus
  155. // ---------------------------------------------------------------------------
  156. //    This function enables menu commands.
  157. //
  158.  
  159. void
  160. CPPStarterApp::FindCommandStatus(
  161.     CommandT    inCommand,
  162.     Boolean        &outEnabled,
  163.     Boolean        &outUsesMark,
  164.     Char16        &outMark,
  165.     Str255        outName)
  166. {
  167.  
  168.     switch (inCommand) {
  169.     
  170.         // Return menu item status according to command messages.
  171.         // Any that you don't handle will be passed to LApplication
  172.  
  173.         case cmd_New:                    // EXAMPLE
  174.             outEnabled = true;            // enable the New command
  175.             break;
  176.  
  177.         default:
  178.             LApplication::FindCommandStatus(inCommand, outEnabled,
  179.                                                 outUsesMark, outMark, outName);
  180.             break;
  181.     }
  182. }
  183.  
  184.  
  185. // ---------------------------------------------------------------------------
  186. //        • ProcessNextEvent
  187. // ____________________________________________________________________________
  188. //
  189. void
  190. CPPStarterApp::ProcessNextEvent()
  191. {
  192.     long status;
  193.     Str31    word = "";
  194. //    if(( gInitGlobals->lWriteBuf > gInitGlobals->lReadBuf ) || 
  195. //        (( gInitGlobals->lWriteBuf == 0 ) && ( gInitGlobals->lReadBuf > 1 )))
  196. //    {
  197. //        Speak( gInitGlobals->asBuf[gInitGlobals->lReadBuf]);
  198. //        gInitGlobals->asBuf[gInitGlobals->lReadBuf][0] = 0;
  199. //        gInitGlobals->lReadBuf++;
  200. //        if( gInitGlobals->lReadBuf > 4) gInitGlobals->lReadBuf = 0;
  201. //    }
  202.     if (gConnID > 0) status = OTdspStatus(gConnID);
  203.     if(status == 10)
  204.     {
  205.         OTRecvText( gConnID, word);
  206.         Speak( word );
  207.     }
  208.     LApplication::ProcessNextEvent();
  209. }
  210.  
  211. // ----------------------------------------------------------------------------
  212. //         • Speak
  213. // ----------------------------------------------------------------------------
  214.  
  215. void
  216. CPPStarterApp::Speak(Str31 word)
  217. {
  218.     OSErr    err;
  219. //    VoiceDescription    defaultVoiceInfo;
  220.  
  221.     
  222. //    StopSpeaking();    //kill current speech if any
  223.     
  224.     //get default voice
  225. //    err = GetVoiceDescription(nil, &defaultVoiceInfo, sizeof(defaultVoiceInfo));
  226. //    if (noErr != err) {
  227. //        err = MakeVoiceSpec('\0\0\0\0', '\0\0\0\0', &defaultVoiceInfo.voice);
  228. //    }
  229.     /* Get a speech channel */
  230.     
  231. //    gSpeakingVoiceIndex = gSelectedVoiceIndex;
  232. //    MoveHHi((Handle)gVoices);
  233. //    HLock((Handle)gVoices);
  234. //    err = NewSpeechChannel(&defaultVoiceInfo.voice, &gSpeechChannel);
  235. //    HUnlock((Handle)gVoices);
  236. //    if (noErr != err) {
  237.     
  238.         /* The voice that the user selected doesn't work; let's try the rest */
  239.         
  240. //        HLock((Handle)gVoices);
  241. //        for (gSpeakingVoiceIndex = 0; gSpeakingVoiceIndex < gNumberVoices; gSpeakingVoiceIndex++) {
  242. //            err = NewSpeechChannel(&(*gVoices)[gSpeakingVoiceIndex], &gSpeechChannel);
  243. //            if (noErr == err) {
  244. //                break;
  245. //            }
  246. //        }
  247. //        HUnlock((Handle)gVoices);
  248. //        if (gSpeakingVoiceIndex == gNumberVoices) {
  249. //            StopSpeaking();
  250. //            break;
  251. //        }
  252. //    }
  253.     
  254.     /* Start speaking the new text */
  255.     
  256.     err = SpeakText(gSpeechChannel, (char *)&word[1], word[0]);
  257.     if (noErr != err) {
  258.         StopSpeaking();
  259.     }
  260.     
  261. }
  262.